package org.openfans.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.openfans.domain.Facade;
import org.openfans.domain.Tag;
import org.openfans.domain.logic.TagRelationManager;
import org.openfans.util.ThreadFans;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class AddTagRelationController extends AbstractController {
	private Facade facade = null;

	public void setFacade(Facade facade) {
		this.facade = facade;
	}

	private TagRelationManager tagRelationManager = null;

	public void setTagRelationManager(TagRelationManager tagRelationManager) {
		this.tagRelationManager = tagRelationManager;
	}

	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ThreadFans.authorize();
		String tagId = request.getParameter("id");
		Tag tag = facade.getTag(Integer.parseInt(tagId));
		String rtagTitle = request.getParameter("rtagTitle");
		if (rtagTitle != null && !"".equals(rtagTitle)) {
			tagRelationManager.addRelations(tag, rtagTitle);
		}
		return new ModelAndView("redirect:viewTag.html?id=" + tagId);
	}
}
